﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace unite
{
    public partial class CalibrationFrm : Form
    {
        HDevelopExport hDevelop = new HDevelopExport();
        private HTuple WindowID;
        HObject ho_Cross;
        private string[] FilePath = null;
        string CameraType;
        public CalibrationFrm()
        {
            InitializeComponent();
            HOperatorSet.OpenWindow(0,0,this.hWindowControl1.Width,this.hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "",out WindowID);
        }

        private void CalibrationFrm_Load(object sender, EventArgs e)
        {
            HOperatorSet.SetPart(WindowID, 0, 0, this.hWindowControl1.Width - 1, this.hWindowControl1.Height - 1);
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.SetColor(WindowID, "green");
            HOperatorSet.DispCross(WindowID, this.hWindowControl1.Width / 2, hWindowControl1.Height / 2, hWindowControl1.Width, 0);
            ho_Cross.Dispose();
            HOperatorSet.GenCrossContourXld(out ho_Cross, this.hWindowControl1.Width / 2, this.hWindowControl1.Height / 2, hWindowControl1.Width, 0);
        }

        private void Open_Image_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Multiselect = true;
            openFile.InitialDirectory = @"C:\Users\AIERXUAN\Documents";
            openFile.Filter = "Image (*.bmp *.tif)|*.bmp;*.tif";
            openFile.RestoreDirectory = true;//还原为之前的选定目录
            if (openFile.ShowDialog() == DialogResult.OK)//点击确定按钮
            {
                FilePath = openFile.FileNames;
            }
        }
        Thread Calibration;
        public static AutoResetEvent odd = new AutoResetEvent(false);
        private void Calibration_btn_Clic(object sender, EventArgs e)
        {
            switch (CameraType_txt.SelectedIndex)
            {
                case 0:
                    CameraType = "area_scan_polynomial";
                    break;
                default:
                    CameraType = "area_scan_division";
                    break;
            }

            //static string Save_File = AppDomain.CurrentDomain.BaseDirectory + "User.ini";
            //string DescriptionFile = DescriptionFile_txt.Text.Trim();
            string DescriptionFile = AppDomain.CurrentDomain.BaseDirectory + DescriptionFile_cmb.Text.Trim() + ".descr";
            double Focus = Convert.ToDouble(Focus_txt.Text.Trim())/1000;
            double Thickness = Convert.ToDouble(Thickness_txt.Text.Trim())/1000;
            double Sx = Convert.ToDouble(Sx_txt.Text.Trim() + "e-06");
            double Sy = Convert.ToDouble(Sy_txt.Text.Trim()+"e-06");
            int Cx = Convert.ToInt32(Cx_txt.Text.Trim());
            int Cy = Convert.ToInt32(Cy_txt.Text.Trim());         
            Calibration = new Thread(() => hDevelop.Action(WindowID, FilePath, DescriptionFile, CameraType, Focus, Thickness, Sx, Sy, Cx, Cy));
            Calibration.IsBackground = true;
            Calibration.Start();
        }

        private void Upload_btn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Multiselect = false;
            openFile.InitialDirectory = @"C:\Users\AIERXUAN\Documents";
            openFile.Filter = "描述文件 (*.cpd *.descr)|*.cpd;*.descr";
            openFile.RestoreDirectory = true;//还原为之前的选定目录
            if (openFile.ShowDialog() == DialogResult.OK)//点击确定按钮
            {
                //DescriptionFile_txt.Text = openFile.FileName;
            }
        }

        private void SaveParameters_btn_Clic(object sender, EventArgs e)
        {
            hDevelop.SaveParameters();
            MessageBox.Show("参数保存成功","提示");
        }
    }
}
